<?php

namespace app\home\controller;

use think\App;
use think\facade\Db;

class User extends Common
{
    //获取用户信息
    public function getUserInfo()
    {
        $user = Db::name('user')->where(['id' => $this->user['id']])->field('name,mobile,id_card,sex,images,unit,section')->find();
        return outputJson(1000, 'ok', ['user' => $user]);
    }

    //我的成绩
    public function myGrade()
    {
        $user = Db::name('user')->where(['id' => $this->user['id']])->field('unit,online_grade,offline_grade')->find();
        //成绩排名
        $userGrade = Db::name('user')->where(['unit' => $user['unit']])->field('name,images,unit,online_grade,offline_grade')->select()->toArray();
        foreach ($userGrade as $k => $v) {
            $data[$k]['grade'] = $v['online_grade'] + $v['offline_grade'];
            $data[$k]['name'] = $v['name'];
            $data[$k]['images'] = $v['images'];
        }
        return outputJson(1000, 'ok', ['onlineGrade' => $user['online_grade'], 'offline_grade' => $user['offline_grade'], 'list' => multi_array_sort($data, 'grade')]);
    }

    //学习时长
    public function myLearnTime()
    {
        $learnTime = Db::name('user_learn_time')->where(['uid' => $this->user['id']])->select()->toArray();
        $dayLearnTime = 0;
        $totalLearnTime = 0;
        foreach ($learnTime as $k => $val) {
            $totalLearnTime += $val['learn_time'];
            if ($val['add_time'] == date('Y-m-d')) {
                $dayLearnTime += $val['learn_time'];
            }
        }
        $learnTime = Db::name('user_learn_time')->where(['uid' => $this->user['id'], 'add_time' => date('Y-m-d')])->field('sum(learn_time) learn_time,type,upid_title')->group('upid')->select()->toArray();

        return outputJson(1000, 'ok', ['dayLearnTime' => $dayLearnTime, 'totalLearnTime' => $totalLearnTime, 'list' => $learnTime]);
    }

    //错题集详情
    public function wrongTopicDetail()
    {
        $table = input('type') == 1 ? 'user_online_wrong_topic' : 'user_offline_wrong_topic';
        $wrongTopicDetail = Db::name($table)->where(['uid' => $this->user['id'], 'id' => input('id')])->field('id,title,type,images,options,answers,analysis')->find();
        $wrongTopicDetail['options'] = json_decode($wrongTopicDetail['options'], true);
        $wrongTopicDetail['answers'] = array_filter(explode('.', trim(chunk_split($wrongTopicDetail['answers'], 1, '.'))));
        $lidtype = input('type');
        $lid = Db::name($table)->where(['uid' => $this->user['id'], 'id' => ['<', input('id')]])->field('max(id) id')->find();
        if ($lid == input('id')) {
            $table1 = input('type') == 1 ? 'user_offline_wrong_topic' : 'user_online_wrong_topic';
            $lidtype = input('type') == 1 ? 2 : 1;
            $lid = Db::name($table1)->where(['uid' => $this->user['id']])->field('id')->order('id desc')->find();
            if(!$lid){
                $lid = input('id');
            }
        }
        $ridtype = input('type');
        $rid = Db::name($table)->where(['uid' => $this->user['id'], 'id' => ['>', input('id')]])->field('max(id) id')->find();
        if ($rid == input('id')) {
            $table1 = input('type') == 1 ? 'user_offline_wrong_topic' : 'user_online_wrong_topic';
            $ridtype = input('type') == 1 ? 2 : 1;
            $rid = Db::name($table1)->where(['uid' => $this->user['id']])->field('id')->order('id asc')->find();
            if(!$rid){
                $rid = input('id');
            }
        }

        return outputJson(1000, 'ok', ['lid' => ['type' => $lidtype ,'id'=> $lid],'rid' => ['type' => $ridtype ,'id'=> $rid],'wrongTopicDetail' => $wrongTopicDetail]);
    }
}